$(function() {	
	//初始化BTN
	initBtnUI();
	
	//初始化窗口
	initWindowUI();
	
	//初始化数据表
	initDataGridUI();
	
});

//初始化BTN
function initBtnUI(){
	//流程任务BTN
	$('#editWorkflowTasksBtn').linkbutton({plain:true,iconCls:'icon-edit'});
	$('#saveWorkflowTasksBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#entrustManagerBtn').linkbutton({plain:true,iconCls:'icon-task'});
	$('#workflowTaskEventBtn').linkbutton({plain:true,iconCls:'icon-task'});
	$('#closeWorkflowTasksBtn').linkbutton({plain:true,iconCls:'icon-close'});
}

//初始化窗口
function initWindowUI(){
	//初始化任务节点窗口
	$('#workflowTasksWindow').window({
		title:'任务节点',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-task',
		width:800,
		height:600
	});
}

//初始化数据表
function initDataGridUI(){
	//初始化任务节点数据表格
	$('#workflowTasksDataGrid').datagrid({
		url:'',
		idField:'id',
		sortName: 'id',
		sortOrder: 'desc',
		border:false,
		nowrap: false,
		striped: true,
		remoteSort:false,
		rownumbers:true,
		pagination:true,
		collapsible:true,
		singleSelect:true,
		toolbar:'#workflowTasksToolBar',
		columns:[[
			{field:'taskNodeName', title:'节点名称', width:200},
			{field:'ableFileds', title:'可编辑字段', width:200, editor:'text'},
			{field:'ableButtons', title:'可显示按钮', width:200, editor: {type:'combobox', options:{url:'workflowTasksAjax!loadWorkflowButtons', valueField:'id', textField:'name', editable:false, multiple:true}}}
		]]
	});

	//分页
	var wtPager = $('#workflowTasksDataGrid').datagrid('getPager');
	$(wtPager).pagination({
		pageSize: 15,
		pageList: [15, 30, 50, 100],
		beforePageText: '第',
		afterPageText: '页    共 {pages} 页',
		displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		onSelectPage:function(pageNumber, pageSize){
			var row = $('#workflowProcessFunMountDataGrid').datagrid('getSelected');
			$('#workflowTasksDataGrid').datagrid('options').url = 'workflowTasksAjax!loadWorkflowTasksData?id=' + row.id + '&pageNo=' + pageNumber + '&pageSize='+ pageSize;
			$('#workflowTasksDataGrid').datagrid('reload', pageNumber);
		}
	});
}

//打开任务节点窗口
function openWorkflowTasksWindow(){
	var row = $('#workflowProcessFunMountDataGrid').datagrid('getSelected');
	if(ROW_IS_NOT_NULL(row)){
		$('#workflowTasksDataGrid').datagrid('options').url = 'workflowTasksAjax!loadWorkflowTasksData?id='+ +row.id+'&pageNo=1&pageSize=15';
		$('#workflowTasksDataGrid').datagrid("options").pageNumber = 1;   
		$("#workflowTasksDataGrid").datagrid('reload'); 
		$("#workflowTasksDataGrid").datagrid('getPager').pagination({pageNumber: 1});
		$('#workflowTasksWindow').window('open');
	}
	
}

//编辑任务节点
function editWorkflowTasks(){
	var row = $('#workflowTasksDataGrid').datagrid('getSelected');
	if (ROW_IS_NOT_NULL(row)) {
		var rowIndex = $('#workflowTasksDataGrid').datagrid('getRowIndex', row);
		$('#workflowTasksDataGrid').datagrid('beginEdit', rowIndex);
	}
}

//保存任务节点信息
function saveWorkflowTasks(){
	var rows = $('#workflowTasksDataGrid').datagrid('getRows');
	for ( var i = 0; i < rows.length; i++) {
		$('#workflowTasksDataGrid').datagrid('endEdit', i);
	}
	
	if ($('#workflowTasksDataGrid').datagrid('getChanges').length) {
		var updated = $('#workflowTasksDataGrid').datagrid('getChanges', "updated");
		var effectRow = new Object();
		
		if (updated.length) {
			effectRow["updated"] = JSON.stringify(updated);
		}

		$.post("workflowTasksAjax!save", effectRow,
				function(data) {
						$('#workflowTasksDataGrid').datagrid('reload');
						$.messager.alert("提示", "保存成功!");					
		}, "json").error(function() {
			$('#workflowTasksDataGrid').datagrid('reload');
			$.messager.alert("提示", "保存失败!");
		});
		
	}
}

//关闭任务节点窗口
function closeWorkflowTasksWindow(){
	$('#workflowTasksWindow').window('close');
}